package cookie.library.random.randoms;

import com.rsbuddy.script.methods.Account;
import com.rsbuddy.script.methods.Settings;
import com.rsbuddy.script.methods.Widgets;
import com.rsbuddy.script.task.Task;
import cookie.library.random.Random;

/**
 * @autor Sir Cookie
 */
public class PinManager extends Random {
    public boolean activate() {
        if(Widgets.get(13).isValid() || (Widgets.get(14).isValid() && Widgets.getComponent(14, 34).getText().contains("Yes, "))){
            return true;
        }
        return false;
    }

    private void enter(String pin){
        final int[] components = {6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
		int state = Settings.get(563);
		if (!Widgets.get(13).isValid() || !Widgets.get(759).isValid() || state == 4) {
			return;
		}
		String number = String.valueOf(pin.charAt(state));
		if (Widgets.getComponent(13, components[Integer.valueOf(number)]).click(true)) {
            Task.sleep(800, 1600);
		}
    }

    public int loop() {
        if(Widgets.get(14).isValid() && Widgets.getComponent(14, 34).getText().contains("Yes, ")){
            Widgets.getComponent(14, 34).click(true);
        } else {
            final String pin = Account.getPin();
            if(pin == null || pin.length() != 4){
                log.severe("You must add bank pin to RSBuddy.");
                stop(true);
            }
            enter(pin);
            if (Widgets.get(211).isValid()) {
				Widgets.get(211).getComponent(3).click(true);
			} else if (Widgets.get(217).isValid()) {
                Task.sleep(10500, 12000);
			}
        }
        return activate() ? 500 : -1;
    }
}
